#pragma once

#include <windows.h>
#include <objbase.h>
#include <zmouse.h>

#include <Shlwapi.h>
#include <ShlObj_core.h>
#pragma comment(lib,"shlwapi.lib")


namespace Utils
{
	class OSUtils
	{
	public:

		static void ShowConsole()
		{
			AllocConsole();
			freopen("CONOUT$", "w+t", stdout);
			freopen("CONIN$", "r+t", stdin);
		}

		static void CloseConsole()
		{
			FreeConsole();
		}

		static CDuiString OpenSelectedDirDialog(HWND hWnd, LPCTSTR defaultPath, LPCTSTR title)
		{
			TCHAR file[MAX_PATH] = _T("");

			BROWSEINFO bif = { 0 };
			bif.hwndOwner = hWnd;
			bif.lpszTitle = title;
			bif.pszDisplayName = file;
			bif.ulFlags = BIF_BROWSEINCLUDEFILES;
			bif.lpfn = BrowserCallback;
			bif.lParam = (LPARAM)defaultPath;

			if (LPITEMIDLIST pil = SHBrowseForFolder(&bif)) {
				SHGetPathFromIDList(pil, file);
				return file;
			}
			return _T("");
		}

	private:
		static int CALLBACK BrowserCallback(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
		{
			switch (uMsg)
			{
			case BFFM_INITIALIZED:
			    {
			        ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)lpData);
			    }
			    break;
			default:
			    break;
			}
			return 0;
		}
	};
}